<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h3>利用数组实现栈结构</h3></br>
<h3> 应用：十进制转二进制</h3>
    <script>
        //类 进行封装
        class Stack {
            constructor() {
                    this.item = [];
                }
                //push()
            push(element) {
                    this.item.push(element);
                }
                //pop()
            pop() {
                    return this.item.pop();
                }
                //peek()
            peek() {
                    return this.item[this.size() - 1];
                }
                //size()
            size() {
                return this.item.length;
            }
            isEmpty() {
                return this.size() === 0;
            }
            toString() {
                return this.item.toString();
            }
        }
        const stack = new Stack();
        stack.push("cn");
        stack.push("us");
        stack.push("jp");
        console.log(stack.toString());
        console.log(stack.pop());
        console.log(stack.toString());
        console.log(stack.peek());
        console.log(stack.size());
        console.log(stack.isEmpty());
    </script>
</body>

</html>